package com.atguigu.gmall.activity.config;

import com.github.benmanes.caffeine.cache.Cache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: atguigu
 * @create: 2023-08-16 09:36
 */
@Component
@Slf4j
public class MessageReceive {


    @Autowired
    private Cache<String, String> seckillCache;

    /**
     * 发布定于客户端监听到消息后业务逻辑
     * 得到商品状态。将其放入当前节点本地缓存中
     *
     * @param state 状态形式  ""31:1""
     */
    public void receiveSeckillGoodState(String state) {
        if (StringUtils.isNotBlank(state)) {
            log.info("监听到Redis发布定于话题：seckillpush中消息：{}", state);
            state = state.replaceAll("\"", "");
            String[] split = state.split(":");
            if (split != null && split.length == 2) {
                seckillCache.put(split[0], split[1]);
            }
        }
    }
}
